-module(xengine_web_launch).
-export([launch/3]).


-include("xengine.hrl").


launch( Module, Fun, Args)->

        Parames=ets:new(xengine_web_launch,[ordered_set,private]),
        ets:insert(Parames, Args),
        case { Module , Fun } of
                {container_manager, stop_container } ->
                         case ets:lookup(Parames,"container_name") of
                                [] ->
                                        { error , "no such a container named : "++ container_name} ;
                                [{_,ContainerName}]  ->
                                        container_manager:stop_container(ContainerName)
                         end;
                %%    [container, [app_name , term_runtime] ]
                {instance_manager, query_instance_term } ->
                        instance_manager:query_instance_term();
                { instance_manager , query_app } ->
                         case ets:lookup(Parames,"app_name") of
                                [] ->
                                        { error , "need app_name"} ;
                                [{_,AppName}]  ->
                                        instance_manager:query_app(AppName)
                         end;

                { instance_manager , query_instance } ->
                         [{"container_name",ContainerName}]=ets:lookup(Parames,"container_name"),
                         instance_manager:query_instance(ContainerName);
                        
                { app_manager, query_app } ->
                        app_manager:query_app();
                %%  ContainerRuntime,[App , Runtime , TotalRuntime]                        
                { container_manager , query_container } ->
        %%                error_logger:info_msg("Module=~p,Fun=~p,Args=~p~n",[Module,Fun,Args]),
                        case ets:lookup(Parames,"name") of
                              [{"name",Name}] ->
                                           container_manager:query_container(Name);
                              [] ->
                                            container_manager:query_container()                                         
                        end;
                { container_manager, add_container } ->
                        [{"name",Name}]=ets:lookup(Parames,"name"),
                        [{"host",Host}]=ets:lookup(Parames,"host"),
                        [{"ip",Ip}]=ets:lookup(Parames,"ip"),
                        [{"port",Port}]=ets:lookup(Parames,"port"),

                        [{"type",Type}]=ets:lookup(Parames,"type"),
   %%                     [{"user",User}]=ets:lookup(Parames,"user"),
                        [{"home",Home}]=ets:lookup(Parames,"home"),
   %%                     [{"password",Password}]=ets:lookup(Parames,"password"),
                        container_manager:add_container(#container{
                                                        name=Name,
                                                        host=Host,
                                                        ip=Ip,
                                                        port=Port,
                                                        type=Type,
                                                        home=Home
                                      %%                  ,user=User,
                                      %%                  password=Password
                                                        } )          
                        
                        ;
                        
                { instance_manager , publish } ->
                        error_logger:info_msg("Module=~p,Fun=~p,Args=~p~n",[Module,Fun,Args]),
                        [{"name",Name}]=ets:lookup(Parames,"name"),
                        instance_manager:publish(Name);

          %%      { instance_manager , stop_instances } ->
           %%       [{"name",Name}]=ets:lookup(Parames,"name"),
            %%            instance_manager:stop_instances(Name);

                { app_manager , regist } ->
                        %%-record(app, { name , domain , source , target } ).
                        [{"name",Name}]=ets:lookup(Parames,"name"),
                        [{"domain",Domain}]=ets:lookup(Parames,"domain"),
                        [{"source",Source}]=ets:lookup(Parames,"source"),
                        [{"target",Target}]=ets:lookup(Parames,"target"),
                        App=#app{name=Name,domain=Domain,source=Source,target=Target},
                        app_manager:regist(App) ;
                Any ->
        		    error_logger:info_msg("error , no such rest interface:~p~n",[Any]),
						{error , Any }
        end.
